class Solution(object):
    def smallestCommonElement(self, mat):
        """
        :type mat: List[List[int]]
        :rtype: int
        """
        m = len(mat)
        n = len(mat[0])
        p = [0] * m
        k = 1
        now_value = mat[0][0]
        while k < m:
            if mat[k][p[k]] == now_value:
                k += 1
            else:
                while p[k] < n and mat[k][p[k]] < now_value:
                    p[k] += 1
                if p[k] == n:
                    return -1
                if mat[k][p[k]] == now_value:
                    k += 1
                else:
                    now_value = mat[k][p[k]]
                    while p[0] < n and mat[0][p[0]] < now_value:
                        p[0] += 1
                    if p[0] == n:
                        return -1
                    else:
                        now_value = mat[0][p[0]]
                        k = 1
        return now_value








